using MicroMsg.Common.Event;
using MicroMsg.UI.Util;
using Microsoft.Phone.UserData;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows.Media.Imaging;
namespace MicroMsg.Common.Utils
{
	public class InfoQuery
	{
		private const string TAG = "InfoQuery";
		private const int NO_HEADIMAGE = 0;
		private const int HAS_HEADIMAGE = 1;
		private static List<string> mCurrentNumberList;
		private static IEnumerable<Contact> mContactEnum;
		private static Dictionary<string, Stream> currentStreamMap;
		public static void GetAllPhoneNum(EventHandler<PhoneNumEventArgs> completeEvent)
		{
			Contacts contacts = new Contacts();
			contacts.add_SearchCompleted(new EventHandler<ContactsSearchEventArgs>(InfoQuery.cons_SearchCompleted));
			contacts.SearchAsync(string.Empty, 0, completeEvent);
		}
		private static List<PhoneNumInfo> GetValidPhoneNumList(ContactsSearchEventArgs contactsSearchEventArgs_0)
		{
			List<PhoneNumInfo> list = new List<PhoneNumInfo>();
			using (IEnumerator<Contact> enumerator = contactsSearchEventArgs_0.get_Results().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Contact current = enumerator.get_Current();
					if (current != null)
					{
						ReadOnlyCollection<ContactPhoneNumber> readOnlyCollection = (ReadOnlyCollection<ContactPhoneNumber>)current.get_PhoneNumbers();
						if (readOnlyCollection != null)
						{
							using (IEnumerator<ContactPhoneNumber> enumerator2 = readOnlyCollection.GetEnumerator())
							{
								while (enumerator2.MoveNext())
								{
									ContactPhoneNumber current2 = enumerator2.get_Current();
									if (current2 != null)
									{
										string numbersFromString = ToolFunction.GetNumbersFromString(current2.get_PhoneNumber());
										if (ToolFunction.checkIsPhoneNumber(numbersFromString))
										{
											PhoneNumInfo phoneNumInfo = new PhoneNumInfo();
											phoneNumInfo.Name = current.get_DisplayName();
											phoneNumInfo.Num = numbersFromString;
											BitmapImage bitmapImage = new BitmapImage();
											Stream picture = current.GetPicture();
											if (picture != null)
											{
												bitmapImage.SetSource(picture);
												phoneNumInfo.imgSourceHead = bitmapImage;
											}
											else
											{
												phoneNumInfo.imgSourceHead = null;
											}
											list.Add(phoneNumInfo);
										}
									}
								}
							}
						}
					}
				}
			}
			return list;
		}
		private static void cons_SearchCompleted(object sender, ContactsSearchEventArgs e)
		{
			EventHandler<PhoneNumEventArgs> eventHandler = (EventHandler<PhoneNumEventArgs>)e.get_State();
			List<PhoneNumInfo> validPhoneNumList = InfoQuery.GetValidPhoneNumList(e);
			eventHandler.Invoke(sender, new PhoneNumEventArgs
			{
				Result = validPhoneNumList
			});
		}
		public static void getEmailAndPhoneNumList(EventHandler<PhoneNumEventArgs> completeEvent)
		{
			Contacts contacts = new Contacts();
			contacts.add_SearchCompleted(new EventHandler<ContactsSearchEventArgs>(InfoQuery.onSearchPhoneNumListCompleted));
			contacts.SearchAsync(string.Empty, 0, completeEvent);
		}
		private static PhoneNumEventArgs getListToRet(ContactsSearchEventArgs contactsSearchEventArgs_0)
		{
			List<PhoneNumInfo> list = new List<PhoneNumInfo>();
			List<PhoneNumInfo> list2 = new List<PhoneNumInfo>();
			int num = 1;
			if (contactsSearchEventArgs_0 != null && contactsSearchEventArgs_0.get_Results() != null)
			{
				Log.smethod_5("InfoQuery", "begin getListToRet");
				using (IEnumerator<Contact> enumerator = contactsSearchEventArgs_0.get_Results().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						Contact current = enumerator.get_Current();
						if (current != null)
						{
							int hasHeadImg = 1;
							ReadOnlyCollection<ContactPhoneNumber> readOnlyCollection = (ReadOnlyCollection<ContactPhoneNumber>)current.get_PhoneNumbers();
							ReadOnlyCollection<ContactEmailAddress> readOnlyCollection2 = (ReadOnlyCollection<ContactEmailAddress>)current.get_EmailAddresses();
							if (readOnlyCollection != null)
							{
								using (IEnumerator<ContactPhoneNumber> enumerator2 = readOnlyCollection.GetEnumerator())
								{
									while (enumerator2.MoveNext())
									{
										ContactPhoneNumber current2 = enumerator2.get_Current();
										if (current2 != null)
										{
											string phoneNumber = current2.get_PhoneNumber();
											list.Add(new PhoneNumInfo
											{
												Name = current.get_DisplayName(),
												Num = phoneNumber,
												String_0 = string.Concat(num),
												hasHeadImg = hasHeadImg
											});
										}
									}
								}
							}
							if (readOnlyCollection2 != null)
							{
								using (IEnumerator<ContactEmailAddress> enumerator3 = readOnlyCollection2.GetEnumerator())
								{
									while (enumerator3.MoveNext())
									{
										ContactEmailAddress current3 = enumerator3.get_Current();
										if (current3 != null)
										{
											string emailAddress = current3.get_EmailAddress();
											list2.Add(new PhoneNumInfo
											{
												Name = current.get_DisplayName(),
												Num = emailAddress,
												String_0 = string.Concat(num),
												hasHeadImg = hasHeadImg
											});
										}
									}
								}
							}
							num++;
						}
					}
				}
				return new PhoneNumEventArgs
				{
					Result = list,
					ResultSec = list2
				};
			}
			return null;
		}
		private static void onSearchPhoneNumListCompleted(object sender, ContactsSearchEventArgs e)
		{
			EventHandler<PhoneNumEventArgs> eventHandler = (EventHandler<PhoneNumEventArgs>)e.get_State();
			InfoQuery.mContactEnum = e.get_Results();
			PhoneNumEventArgs listToRet = InfoQuery.getListToRet(e);
			if (eventHandler != null)
			{
				eventHandler.Invoke(sender, listToRet);
			}
			if (InfoQuery.mCurrentNumberList != null)
			{
				InfoQuery.getHeadImgFromContact();
			}
		}
		public static void getCurrentListHeadImg(List<string> numberList)
		{
			InfoQuery.mCurrentNumberList = numberList;
			if (InfoQuery.mContactEnum == null)
			{
				InfoQuery.getEmailAndPhoneNumList(null);
				return;
			}
			InfoQuery.getHeadImgFromContact();
		}
		private static void getHeadImgFromContact()
		{
			if (InfoQuery.mCurrentNumberList != null && InfoQuery.mContactEnum != null)
			{
				try
				{
					InfoQuery.currentStreamMap = new Dictionary<string, Stream>();
					using (List<string>.Enumerator enumerator = InfoQuery.mCurrentNumberList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							string current = enumerator.get_Current();
							InfoQuery.currentStreamMap.Add(current, null);
						}
					}
					using (IEnumerator<Contact> enumerator2 = InfoQuery.mContactEnum.GetEnumerator())
					{
						while (enumerator2.MoveNext())
						{
							Contact current2 = enumerator2.get_Current();
							if (current2 != null && current2.GetPicture() != null)
							{
								ReadOnlyCollection<ContactPhoneNumber> readOnlyCollection = (ReadOnlyCollection<ContactPhoneNumber>)current2.get_PhoneNumbers();
								if (readOnlyCollection != null)
								{
									using (IEnumerator<ContactPhoneNumber> enumerator3 = readOnlyCollection.GetEnumerator())
									{
										while (enumerator3.MoveNext())
										{
											ContactPhoneNumber current3 = enumerator3.get_Current();
											if (current3 != null)
											{
												string numbersFromString = ToolFunction.GetNumbersFromString(current3.get_PhoneNumber());
												if (InfoQuery.currentStreamMap.ContainsKey(numbersFromString))
												{
													InfoQuery.currentStreamMap.set_Item(numbersFromString, current2.GetPicture());
													break;
												}
											}
										}
									}
								}
							}
						}
					}
				}
				catch (Exception ex)
				{
					Log.smethod_1("InfoQuery", ex.ToString());
					InfoQuery.currentStreamMap = null;
				}
				EventCenter.postEvent(EventConst.ON_GET_CURRENT_HEADIMG_COMPLETE, InfoQuery.currentStreamMap, null);
				return;
			}
		}
	}
}
